package com.qianfeng.smartdevice.controller;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.pojo.Category;
import com.qianfeng.smartdevice.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * Created by jackiechan on 2020/12/9 23:40
 *
 * @Author jackiechan
 */
@RestController
@RequestMapping("/categories")
@Api(tags = "分类管理模块" )
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("添加分类")
    @PostMapping("/category")
    public R addCategory(@RequestBody Category category){
        categoryService.addCategory(category);
        return R.setOK(null);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("根据 id 查询分类")
    public R findCategoryById(@ApiParam(name = "id",value = "主键",defaultValue = "1")@PathVariable Long id) {
        Category category = categoryService.getCategoryById(id);
        return R.setOK(category);
    }

    @ApiOperation("分页查询分类")
    @GetMapping("/categories")
    public R findAllCategories(@ApiParam(name = "page",value = "第几页",defaultValue = "1")@RequestParam(defaultValue = "1") int page,@ApiParam(name = "limit",value = "每页数量",defaultValue = "10") @RequestParam(defaultValue = "10") int limit,@ApiParam(name = "categoryname",value = "分类模糊名字",example = "ESP8266") @RequestParam(required = false) String categoryname, @ApiParam(name = "status",value = "状态",defaultValue = "-100") @RequestParam(defaultValue = "-100") String status) {
        PageInfo<Category> pageInfo = categoryService.getAllCategoriesByPage(page, limit,categoryname,status);
        return R.setOK(pageInfo);
    }


    @PutMapping("/category")
    @ApiOperation("更新分类")
    public R updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return R.setOK(null);
    }

    @DeleteMapping("/categories")
    @ApiOperation("删除分类")
    public R deleteCategories(@ApiParam(name = "ids",required = true,value = "主键") @RequestParam String[] ids) {
        categoryService.deleteCategories(Arrays.asList(ids));
        return R.setOK(null);
    }





}
